package com.dalongxin.work;

import android.app.Application;
import android.os.Environment;

import com.dalongxin.work.data.User;
import com.dalongxin.work.utils.SharedPreferencesUtils;
import com.dalongxin.work.utils.ToastUtil;
import com.dalongxin.work.utils.Utils;

import org.xutils.DbManager;
import org.xutils.common.util.LogUtil;
import org.xutils.db.Selector;
import org.xutils.db.sqlite.WhereBuilder;
import org.xutils.db.table.TableEntity;
import org.xutils.ex.DbException;
import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.io.File;

public class BaseApplication extends Application {

    private volatile static BaseApplication instance;
    private User defaultAccount;
    private SharedPreferencesUtils spUtils;

    public static BaseApplication getInstance() {
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        x.Ext.init(this);
        x.Ext.setDebug(true);
        ToastUtil.init(this);
        instance = this;
        spUtils = new SharedPreferencesUtils(this, "DLX");
    }

    public ImageOptions.Builder getDefaultImageOption() {
        /**
         * 通过ImageOptions.Builder().set方法设置图片的属性
         */
        ImageOptions.Builder builder = new ImageOptions.Builder();//的一些其他属性：
        ImageOptions options = builder.setFadeIn(true).build(); //淡入效果
        //.setCircular(true) //设置图片显示为圆形
        //.setSquare(true) //设置图片显示为正方形
        //setCrop(true).setSize(200,200) //设置大小
        //.setAnimation(animation) //设置动画
        //.setFailureDrawable(Drawable failureDrawable) //设置加载失败的动画
        //.setFailureDrawableId(int failureDrawable) //以资源id设置加载失败的动画
        //.setLoadingDrawable(Drawable loadingDrawable) //设置加载中的动画
        //.setLoadingDrawableId(int loadingDrawable) //以资源id设置加载中的动画
        //.setIgnoreGif(false) //忽略Gif图片
        //.setParamsBuilder(ParamsBuilder paramsBuilder) //在网络请求中添加一些参数
        //.setRaduis(int raduis) //设置拐角弧度
        //.setUseMemCache(true) //设置使用MemCache，默认true
        return builder;
    }

    public User getDefaultAccount() {
        defaultAccount = spUtils.getObject(Constants.Key.USER, User.class);
        return defaultAccount;
    }

    public void setDefaultAccount(User account) {
        if(account != null) {
            spUtils.setObject(Constants.Key.USER, account);
        }
        this.defaultAccount = account;
    }

    public void clearDefaultAccount() {
        spUtils.setObject(Constants.Key.USER, "");
    }
}
